package com.zysd.collection.scheduler.controller;

import com.lensyn.collect.common.enums.CollectInfoGroup;
import com.lensyn.collect.common.enums.CollectInfos;
import com.lensyn.collect.common.enums.TaskType;
import com.lensyn.common.utils.system.response.Response;
import com.zysd.collection.scheduler.condition.TaskModelCondition;
import com.zysd.collection.scheduler.entity.SchedulerTaskModel;
import com.zysd.collection.scheduler.service.model.SchedulerTaskModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务模板管理
 *
 * @author liuht
 * @date 2017/10/11 16:05
 */
@RestController
@RequestMapping("/scheduler")
public class SchedulerModelController {

    @Autowired
    private SchedulerTaskModelService modelService;

    /**
     * 分页查询任务模板
     * @return 分页数据
     */
    @RequestMapping(value = "/models/page", method = RequestMethod.GET)
    public Response models(TaskModelCondition condition) {
        if (!condition.checkPageParam()) {
            return new Response().failure("分页查询条件不能为空");
        }
        return new Response<>().success(modelService.getListPage(condition));
    }

    /**
     * 查询模板详情
     * @param id 主键
     * @return 模板详情
     */
    @RequestMapping(value = "/model/{id}", method = RequestMethod.GET)
    public Response getModel(@PathVariable Long id) {
        return new Response<>().success(modelService.getById(id));
    }

    /**
     * 查询任务模板 信息类
     * @param taskType 任务对象类型
     * @return 模板详情
     */
    @RequestMapping(value = "/model/infos/{taskType}", method = RequestMethod.GET)
    public Response getInfos(@PathVariable String taskType) {
        TaskType type = TaskType.forCode(taskType);
        if (type == null) {
            return new Response().failure("任务对象类型无效!");
        }
        List<CollectInfos> infos = CollectInfos.groupBy(type);
        List<Map<String, String>> result = new ArrayList<>(infos.size());
        for (CollectInfos info : infos) {
            Map<String, String> json = new HashMap<>(2);
            json.put("code", info.getCode());
            json.put("desc", info.getDesc());
            result.add(json);
        }

        return new Response<>().success(result);
    }

    /**
     * 查询所有任务模板信息类 按{@link CollectInfoGroup} 分组返回
     * @return 模板详情
     */
    @RequestMapping(value = "/model/infos", method = RequestMethod.GET)
    public Response getInfos() {
        CollectInfoGroup[] groups = CollectInfoGroup.values();
        Map<String, List> result = new HashMap<>(groups.length);
        for (CollectInfoGroup group : groups) {
            List<CollectInfos> infos = CollectInfos.groupBy(group);
            List<Map<String, String>> infoList = new ArrayList<>(infos.size());
            for (CollectInfos info : infos) {
                Map<String, String> json = new HashMap<>(2);
                json.put("code", info.getCode());
                json.put("desc", info.getDesc());
                infoList.add(json);
            }
            result.put(group.getDesc(), infoList);
        }
        return new Response<>().success(result);
    }

    /**
     * 禁用模板
     * @param id 主键
     * @return 调用结果
     */
    @RequestMapping(value = "/model/disable/{id}", method = RequestMethod.PATCH)
    public Response disableModel(@PathVariable Long id) {
        modelService.disableModel(id);
        return new Response().success();
    }

    /**
     * 启用模板
     * @param id 主键
     * @return 调用结果
     */
    @RequestMapping(value = "/model/enable/{id}", method = RequestMethod.PATCH)
    public Response enableModel(@PathVariable Long id) {
        modelService.enableModel(id);
        return new Response().success();
    }

    /**
     * 更新模板
     * @param model 模板数据
     * @return 调用结果
     */
    @RequestMapping(value = "/model/update", method = RequestMethod.PATCH)
    public Response updateModel(@RequestBody @Valid SchedulerTaskModel model, BindingResult result) {
        if (StringUtils.isEmpty(model.getId())) {
            return new Response().failure("更新失败: 模板编号为空");
        }
        if (result.hasErrors()) {
            return new Response().failure(result.getFieldError().getDefaultMessage());
        }
        modelService.updateModelByid(model);
        return new Response().success();
    }

    /**
     * 新增模板
     * @param model 模板数据
     * @return 调用结果
     */
    @RequestMapping(value = "/model/add", method = RequestMethod.POST)
    public Response addModel(@RequestBody @Valid SchedulerTaskModel model, BindingResult result) {
        if (result.hasErrors()) {
            return new Response().failure(result.getFieldError().getDefaultMessage());
        }
        modelService.addModel(model);
        return new Response().success();
    }
}
